<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="ID" prop="id">
        <el-input
          v-model="queryParams.id"
          placeholder="请输入ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="编号" prop="uuid">
        <el-input
          v-model="queryParams.uuid"
          placeholder="请输入编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联文档ID" prop="documentId">
        <el-input
          v-model="queryParams.documentId"
          placeholder="请输入关联文档ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问题" prop="question">
        <el-input
          v-model="queryParams.question"
          placeholder="请输入问题"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="答案" prop="answer">
        <el-input
          v-model="queryParams.answer"
          placeholder="请输入答案"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <!-- 以下搜索字段已注释，可在后期需要时取消注释使用 -->
      <!-- <el-form-item label="审核人id" prop="auditUser">
        <el-input
          v-model="queryParams.auditUser"
          placeholder="请输入审核人id"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="审核人姓名" prop="auditUserName">
        <el-input
          v-model="queryParams.auditUserName"
          placeholder="请输入审核人姓名"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="是否审核通过" prop="isAuditPassed">
        <el-select 
          v-model="queryParams.isAuditPassed" 
          placeholder="请选择是否审核通过" 
          clearable
          style="width: 240px"
        >
          <el-option
            v-for="dict in sys_data_annotation_review"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:dataCleanManagement:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:dataCleanManagement:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:dataCleanManagement:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:dataCleanManagement:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dataCleanManagementList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="编号" align="center" prop="uuid" />
      <el-table-column label="关联文档ID" align="center" prop="documentId" />
      <el-table-column label="问题" align="center" prop="question" show-overflow-tooltip>
        <template #default="scope">
          <span class="link-text" @click="handleTextEdit(scope.row, 'question')">
            {{ scope.row.question?.length > 30 ? scope.row.question.substring(0, 30) + '...' : scope.row.question }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="答案" align="center" prop="answer" show-overflow-tooltip>
        <template #default="scope">
          <span class="link-text" @click="handleTextEdit(scope.row, 'answer')">
            {{ scope.row.answer?.length > 30 ? scope.row.answer.substring(0, 30) + '...' : scope.row.answer }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="人工审核结果" align="center" prop="peopleAuditResult" show-overflow-tooltip>
        <template #default="scope">
          <span>{{ scope.row.peopleAuditResult?.length > 30 ? scope.row.peopleAuditResult.substring(0, 30) + '...' : scope.row.peopleAuditResult }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否审核通过" align="center" prop="isAuditPassed">
        <template #default="scope">
          <dict-tag :options="sys_data_annotation_review" :value="scope.row.isAuditPassed"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:dataCleanManagement:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:dataCleanManagement:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据清洗管理对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="dataCleanManagementRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联文档ID" prop="documentId">
          <el-input v-model="form.documentId" placeholder="请输入关联文档ID" />
        </el-form-item>
        <el-form-item label="问题" prop="question">
          <el-input v-model="form.question" placeholder="请输入问题" />
        </el-form-item>
        <el-form-item label="答案" prop="answer">
          <el-input v-model="form.answer" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="数据来源" prop="dataSource">
          <el-input v-model="form.dataSource" placeholder="请输入数据来源" />
        </el-form-item>
        <el-form-item label="标注机器人" prop="annotationRobot">
          <el-input v-model="form.annotationRobot" placeholder="请输入标注机器人" />
        </el-form-item>
        <el-form-item label="审核机器人" prop="auditRobot">
          <el-input v-model="form.auditRobot" placeholder="请输入审核机器人" />
        </el-form-item>
        <el-form-item label="评估机器人" prop="accessRobot">
          <el-input v-model="form.accessRobot" placeholder="请输入评估机器人" />
        </el-form-item>
        <el-form-item label="人工审核结果" prop="peopleAuditResult">
          <el-input v-model="form.peopleAuditResult" placeholder="请输入人工审核结果" />
        </el-form-item>
        <el-form-item label="机构人参考文档" prop="referenceDoc">
          <el-input v-model="form.referenceDoc" placeholder="请输入机构人参考文档" />
        </el-form-item>
        <el-form-item label="代理人" prop="agentName">
          <el-input v-model="form.agentName" placeholder="请输入代理人" />
        </el-form-item>
        <el-form-item label="是否审核通过" prop="isAuditPassed">
          <el-select v-model="form.isAuditPassed" placeholder="请选择是否审核通过" style="width: 240px">
            <el-option
              v-for="dict in sys_data_annotation_review"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>

    <!-- 文本编辑对话框 -->
    <el-dialog :title="textEditTitle" v-model="textEditOpen" width="600px" append-to-body>
      <el-form ref="textEditRef" :model="textEditForm">
        <el-form-item>
          <el-input
            v-model="textEditForm.content"
            type="textarea"
            :rows="10"
            placeholder="请输入内容"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitTextEdit">保 存</el-button>
          <el-button @click="cancelTextEdit">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="DataCleanManagement">
import { listDataCleanManagement, getDataCleanManagement, delDataCleanManagement, addDataCleanManagement, updateDataCleanManagement } from "@/api/system/dataCleanManagement";

const { proxy } = getCurrentInstance();
const { sys_data_annotation_review } = proxy.useDict("sys_data_annotation_review");

const dataCleanManagementList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const textEditTitle = ref("");
const textEditOpen = ref(false);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    id: null,
    uuid: null,
    documentId: null,
    auditUser: null,
    auditUserName: null,
    isAuditPassed: undefined
  },
  rules: {
    documentId: [
      { required: true, message: "关联文档ID不能为空", trigger: "blur" }
    ],
    question: [
      { required: true, message: "问题不能为空", trigger: "blur" }
    ],
    answer: [
      { required: true, message: "答案不能为空", trigger: "blur" }
    ],
  },
  textEditForm: {}
});

const { queryParams, form, rules, textEditForm } = toRefs(data);

/** 查询数据清洗管理列表 */
function getList() {
  loading.value = true;
  listDataCleanManagement(queryParams.value).then(response => {
    dataCleanManagementList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    uuid: null,
    documentId: null,
    question: null,
    answer: null,
    dataSource: null,
    createTime: null,
    auditTime: null,
    auditUser: null,
    auditUserName: null,
    annotationRobot: null,
    auditRobot: null,
    accessRobot: null,
    peopleAuditResult: null,
    referenceDoc: null,
    updateTime: null,
    agentName: null,
    isAuditPassed: undefined
  };
  proxy.resetForm("dataCleanManagementRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  queryParams.value.isAuditPassed = undefined;
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加数据清洗管理";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getDataCleanManagement(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改数据清洗管理";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["dataCleanManagementRef"].validate(valid => {
    if (valid) {
      if (form.value.id != null) {
        updateDataCleanManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addDataCleanManagement(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除数据清洗管理编号为"' + _ids + '"的数据项？').then(function() {
    return delDataCleanManagement(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/dataCleanManagement/export', {
    ...queryParams.value
  }, `dataCleanManagement_${new Date().getTime()}.xlsx`)
}

// 文本编辑对话框
function handleTextEdit(row, field) {
  textEditTitle.value = `编辑${field === 'question' ? '问题' : '答案'}`;
  textEditForm.value = {
    id: row.id,
    field: field,
    content: row[field]
  };
  textEditOpen.value = true;
}

function submitTextEdit() {
  if (textEditForm.value.content !== undefined) {
    const row = dataCleanManagementList.value.find(item => item.id === textEditForm.value.id);
    if (row) {
      const updateData = {
        id: row.id,
        [textEditForm.value.field]: textEditForm.value.content
      };
      updateDataCleanManagement(updateData).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        getList();
        textEditOpen.value = false;
      });
    }
  }
}

function cancelTextEdit() {
  textEditOpen.value = false;
}

getList();
</script>

<style scoped>
.link-text {
  color: #409EFF;
  cursor: pointer;
}
.link-text:hover {
  text-decoration: underline;
}
</style>
